From: Aron Xu <aron@debian.org>
Date: Mon, 13 Feb 2012 14:57:45 +0800
Subject: send crlf

---
 nc.1     |    6 ++++--
 netcat.c |   21 +++++++++++++++++----
 2 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/nc.1 b/nc.1
index b7014a2..af44976 100644
--- a/nc.1
+++ b/nc.1
@@ -34,7 +34,7 @@
 .Sh SYNOPSIS
 .Nm nc
 .Bk -words
-.Op Fl 46DdhklnrStUuvz
+.Op Fl 46CDdhklnrStUuvz
 .Op Fl I Ar length
 .Op Fl i Ar interval
 .Op Fl O Ar length
@@ -98,6 +98,8 @@ to use IPv4 addresses only.
 Forces
 .Nm
 to use IPv6 addresses only.
+.It Fl C
+Send CRLF as line-ending.
 .It Fl D
 Enable debugging on the socket.
 .It Fl d
@@ -355,7 +357,7 @@ More complicated examples can be built up when the user knows the format
 of requests required by the server.
 As another example, an email may be submitted to an SMTP server using:
 .Bd -literal -offset indent
-$ nc localhost 25 \*(Lt\*(Lt EOF
+$ nc [\-C] localhost 25 \*(Lt\*(Lt EOF
 HELO host.example.com
 MAIL FROM:\*(Ltuser@host.example.com\*(Gt
 RCPT TO:\*(Ltuser2@host.example.com\*(Gt
diff --git a/netcat.c b/netcat.c
index fdaca44..4f4d2bf 100644
--- a/netcat.c
+++ b/netcat.c
@@ -111,6 +111,7 @@
 #define CONNECTION_TIMEOUT 2
 
 /* Command Line Options */
+int     Cflag = 0;                              /* CRLF line-ending */
 int	dflag;					/* detached, no stdin */
 unsigned int iflag;				/* Interval Flag */
 int	jflag;					/* use jumbo frames if we can */
@@ -180,7 +181,7 @@ main(int argc, char *argv[])
 	sv = NULL;
 
 	while ((ch = getopt(argc, argv,
-	    "46DdhI:i:jklnO:P:p:rSs:tT:UuV:vw:X:x:z")) != -1) {
+	    "46CDdhI:i:jklnO:P:p:rSs:tT:UuV:vw:X:x:z")) != -1) {
 		switch (ch) {
 		case '4':
 			family = AF_INET;
@@ -309,6 +310,9 @@ main(int argc, char *argv[])
 			if (Tflag < 0 || Tflag > 255 || errstr || errno)
 				errx(1, "illegal tos value %s", optarg);
 			break;
+                case 'C':
+                        Cflag = 1;
+                        break;
 		default:
 			usage(1);
 		}
@@ -906,8 +910,16 @@ readwrite(int nfd)
 			else if (n == 0) {
 				goto shutdown_wr;
 			} else {
-				if (atomicio(vwrite, nfd, buf, n) != n)
-					return;
+				if ((Cflag) && (buf[n-1]=='\n')) {
+					if (atomicio(vwrite, nfd, buf, n-1) != (n-1))
+						return;
+					if (atomicio(vwrite, nfd, "\r\n", 2) != 2)
+						return;
+				}
+				else {
+					if (atomicio(vwrite, nfd, buf, n) != n)
+						return;
+				}
 			}
 			}
 			else if (pfd[1].revents & POLLHUP) {
@@ -1139,6 +1151,7 @@ help(void)
 	fprintf(stderr, "\tCommand Summary:\n\
 	\t-4		Use IPv4\n\
 	\t-6		Use IPv6\n\
+	\t-C		Send CRLF as line-ending\n\
 	\t-D		Enable the debug socket option\n\
 	\t-d		Detach from stdin\n\
 	\t-h		This help text\n\
@@ -1172,7 +1185,7 @@ void
 usage(int ret)
 {
 	fprintf(stderr,
-	    "usage: nc [-46DdhjklnrStUuvz] [-I length] [-i interval] [-O length]\n"
+	    "usage: nc [-46CDdhjklnrStUuvz] [-I length] [-i interval] [-O length]\n"
 	    "\t  [-P proxy_username] [-p source_port] [-s source] [-T toskeyword]\n"
 	    "\t  [-V rtable] [-w timeout] [-X proxy_protocol]\n"
 	    "\t  [-x proxy_address[:port]] [destination] [port]\n");
-- 
